package cn.HL.basic.controller;

import cn.HL.basic.dto.LoginDto;
import cn.HL.basic.dto.WechatDto;
import cn.HL.basic.exception.BusinessException;
import cn.HL.basic.service.ILoginService;
import cn.HL.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.lang.model.element.VariableElement;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    private AjaxResult accountLogin(@RequestBody LoginDto dto){
        try {

            Map<String, Object> map = loginService.accountLogin(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败！"+e.getMessage());
        }

    }

    @PostMapping("/wechat")
    private AjaxResult wechat(@RequestBody WechatDto dto){
        try {


            return loginService.wechat(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败！"+e.getMessage());
        }

    }

    /**
     * 绑定
     * @param dto
     * @return
     */
    @PostMapping("/wechat/binder")
    private AjaxResult binder(@RequestBody WechatDto dto){
        try {

            Map<String, Object> map = loginService.binder(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败！"+e.getMessage());
        }

    }
}
